package tt.dz.appserver.service.business.imp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.avro.AvroRemoteException;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;

import tt.dz.appserver.entity.model.ResultMappingEntity;
import tt.dz.appserver.entity.vo.OrderDetailVo;
import tt.dz.appserver.entity.vo.OrderInfoVo;
import tt.dz.appserver.entity.vo.PaymentDetail;
import tt.dz.appserver.entity.vo.ReOrderDetailVo;
import tt.dz.appserver.entity.vo.ReservationDetailVo;

import tt.dz.appserver.entity.vo.charge.ChargeDetailVo;
import tt.dz.appserver.entity.vo.charger.ChargerDetailVo;
import tt.dz.appserver.entity.vo.station.StationDetailVo;
import tt.dz.appserver.service.avro.client.main.MSRPCService;
import tt.dz.appserver.service.avro.client.oms.dzttOmsServiceProtocol;
import tt.dz.appserver.service.avro.client.order.dzttOrderServiceProtocol;

import tt.dz.appserver.service.business.FundService;
import tt.dz.appserver.util.CodeUtils;
import tt.dz.appserver.util.Constants;
import tt.dz.appserver.util.Constants.OmsServerResultField;
import tt.dz.appserver.util.Result;
import tt.dz.appserver.util.date.DateUtil;
import tt.dz.appserver.util.dzcode.OmsCode;
import tt.dz.appserver.util.enums.OrderOpResult;
import tt.dz.appserver.util.enums.OrderServiceType;
import tt.dz.appserver.util.enums.Response;
import tt.dz.appserver.util.json.JsonUtils;


/**
 * 
 * 账单业务接口实现类 xielongfei
 *
 */
@Service
public class FundServiceImpl implements FundService {

	/** 日志 */
	private final static Logger LOG = Logger.getLogger("[订单业务接口]:");

	/** 订单服务器业务接口 */
	@Resource(name = "orderMessageProtocolRequestor")
	dzttOrderServiceProtocol orderService;
	
	/**oms服务器业务接口*/
	@Resource(name="omsMessageProtocolRequestor")
	dzttOmsServiceProtocol omsService;
	
	@Resource(name="mainMessageProtocolRequestor")
	MSRPCService msService;
	@Override
	public JSONObject orderList(String userId, String pageNo, String status,String showHide) throws AvroRemoteException {

		JSONObject returnJsonObject = new JSONObject();

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("condition", "2");
		params.put("userId", userId);
		params.put("orderStatus", status);
		params.put("pageNo", pageNo);
		params.put("pageSize", Constants.PAGE_SIZE);
		params.put("showAttach", showHide);

		String resultCharSequence = orderService.orderHistoryQuery(JSON.toJSONString(params)).toString();

		JSONObject orderResultObject = JSONObject.parseObject(resultCharSequence);
		String retCode = orderResultObject.getString("retCode");

		LOG.info("获取【" + userId + "】 用户状态为 【" + (StringUtils.isEmpty(status) ? "所有" : status) + "】 的订单列表,订单服务器返回结果为:" + retCode);

		// 操作
		if (OrderOpResult.SUCCESS.getCode().equals(retCode)) {

			returnJsonObject = JsonUtils.getSuccessJson();

			JSONArray jsonArray = orderResultObject.getJSONArray("bizData");
			List<OrderInfoVo> orderVos = new ArrayList<OrderInfoVo>();

			for (int i = 0; i < jsonArray.size(); i++) {

				// 订单基本信息
				JSONObject fundInfo = jsonArray.getJSONObject(i);
				// 字段封装
				OrderInfoVo orderVo = JsonUtils.JsonToObjectOfClass(fundInfo, OrderInfoVo.class);
				
				String attach=orderVo.getAttach();
				
				if(attach!=null&&!attach.equals("")){
					JSONObject attachJson=JSONObject.parseObject(attach);
					orderVo.setLables(attachJson.getString("lables"));
					orderVo.setEvaluate(attachJson.getString("evaluate"));
					orderVo.setGrade(attachJson.getString("grade"));
					orderVo.setAttach(null);
				}
				
				//在订单详情里获取桩号后放入orderInfoVo
				//默认没有放入桩对象
				boolean  isget=true;

				// 获取订单详细
				JSONArray detailArray = fundInfo.getJSONArray("itemDetials");
				// 订单详细
				List<OrderDetailVo> orderDetailVos = new ArrayList<OrderDetailVo>();
				
				for (int j = 0; j < detailArray.size(); j++) {
					// 订单详细
					JSONObject orderDetailJson = detailArray.getJSONObject(j);
					// 封装订单详细
					OrderDetailVo orderDetailVo = JsonUtils.JsonToObjectOfClass(orderDetailJson, OrderDetailVo.class);
					//查询设备详情
					LOG.info("开始获取设备信息");
					if(isget)
						if(orderDetailVo.getEquipmentNo()!=null&&orderDetailVo.getEquipmentNo().length()>0){
							
							
							String resultJsonString = omsService.getChargerDetailByChargerId(orderDetailVo.getEquipmentNo()).toString();
							
							// json转换
							JSONObject omsResultObject = JSONObject.parseObject(resultJsonString);
	
							// 获取操作结果
							String code = omsResultObject.getString(Constants.OmsServerResultField.CODE);
	
							LOG.info("OMS获取 【" + orderDetailVo.getEquipmentNo() + "】 电桩详细,接口返回代码:" + code);
							LOG.info("获取到桩结果："+resultJsonString);
							// 操作成功
							if (OmsCode.SUCCESS.equals(code)) {
								
								
								JSONObject charger = omsResultObject.getJSONObject("pojo");
								
								// 封装字段
								if(charger!=null){
									ChargerDetailVo chargerDetailVo = JsonUtils.JsonToObjectOfClass(charger, ChargerDetailVo.class);
									orderDetailVo.setCharger(chargerDetailVo);
									orderVo.setCharger(chargerDetailVo);
									isget=false;
								}
							}
						}
					
					if(orderDetailVo.getMessage() != null && (!orderDetailVo.getMessage().equals(""))){
						LOG.info("获取到订单详情message=【"+orderDetailVo.getMessage()+"】");
						JSONArray message = JSON.parseArray(orderDetailVo.getMessage());
						List<PaymentDetail> details= Lists.newArrayList();
						
						for(int p=0;p<message.size();p++){
							PaymentDetail detail = JsonUtils.JsonToObjectOfClass(message.getJSONObject(p), PaymentDetail.class);
							details.add(detail);
						}
						orderDetailVo.setDetails(details);
					}
					
					// 加入详细集合
					orderDetailVos.add(orderDetailVo);
				}

				// 设置订单详细
				orderVo.setOrderDetails(orderDetailVos);

				orderVos.add(orderVo);
				/**
				 * TODO
				 *  如果遇到订单查不到设备号，则不显示这条订单
				 */
//				if(orderVo.getCharger()==null){
//					orderVos.remove(i);
//					i--;		
//				}
				
			}
			returnJsonObject.put("fundList", orderVos);
		} else {
			returnJsonObject = JsonUtils.getFailJson(Response.RUNTIME_ERROR);
		}
		LOG.info(returnJsonObject.toJSONString());
		return returnJsonObject;
	}
	
	
	@Override
	public JSONObject orderListOther(String userId, String pageNo, String status) throws AvroRemoteException {

		JSONObject returnJsonObject = new JSONObject();

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("condition", "2");
		params.put("userId", userId);
		params.put("orderStatus", status);
		params.put("pageNo", pageNo);
		params.put("pageSize", Constants.PAGE_SIZE);

		String resultCharSequence = orderService.orderHistoryQuery(JSON.toJSONString(params)).toString();

		JSONObject orderResultObject = JSONObject.parseObject(resultCharSequence);
		String retCode = orderResultObject.getString("retCode");

		LOG.info("获取【" + userId + "】 用户状态为 【" + (StringUtils.isEmpty(status) ? "所有" : status) + "】 的订单列表,订单服务器返回结果为:" + retCode);

		// 操作
		if (OrderOpResult.SUCCESS.getCode().equals(retCode)) {

			returnJsonObject = JsonUtils.getSuccessJson();

			JSONArray jsonArray = orderResultObject.getJSONArray("bizData");
			List<OrderInfoVo> orderVos = new ArrayList<OrderInfoVo>();

			for (int i = 0; i < jsonArray.size(); i++) {

				// 订单基本信息
				JSONObject fundInfo = jsonArray.getJSONObject(i);
				// 字段封装
				OrderInfoVo orderVo = JsonUtils.JsonToObjectOfClass(fundInfo, OrderInfoVo.class);

				// 获取订单详细
				JSONArray detailArray = fundInfo.getJSONArray("itemDetials");
				// 订单详细
				List<OrderDetailVo> orderDetailVos = new ArrayList<OrderDetailVo>();
				for (int j = 0; j < detailArray.size(); j++) {
					// 订单详细
					JSONObject orderDetailJson = detailArray.getJSONObject(j);
					// 封装订单详细
					OrderDetailVo orderDetailVo = JsonUtils.JsonToObjectOfClass(orderDetailJson, OrderDetailVo.class);
					//获取电桩的 站名，桩号，快冲/慢充
					
					
					// 加入详细集合
					orderDetailVos.add(orderDetailVo);
				}

				// 设置订单详细
				orderVo.setOrderDetails(orderDetailVos);
				orderVos.add(orderVo);
			}
			returnJsonObject.put("fundList", orderVos);
		} else {
			returnJsonObject = JsonUtils.getFailJson(Response.RUNTIME_ERROR);
		}
		return returnJsonObject;
	}


	@Override
	public JSONObject orderDetail(String orderNo) throws AvroRemoteException {
		
		JSONObject returnJsonObject = null;

		//查询订单详细
		String resultCharSequence = orderService.findOrderItemsByOrderNo(orderNo).toString();

		JSONObject orderResultObject = JSONObject.parseObject(resultCharSequence);
		String retCode = orderResultObject.getString("retCode");

		LOG.info("获取【" + orderNo + "】 订单详细,订单服务器返回结果为:" + retCode);
		
		// 操作
		if (OrderOpResult.SUCCESS.getCode().equals(retCode)) {

			returnJsonObject = JsonUtils.getSuccessJson();
			
			//详细json
			JSONObject orderDetailParentJson = orderResultObject.getJSONObject("bizData");
			//子项
			JSONArray orderDetailArray = orderDetailParentJson.getJSONArray("itemDetials");
			//返回的数据集
			ReOrderDetailVo reOrderDetailVo = JsonUtils.JsonToObjectOfClass(orderDetailParentJson, ReOrderDetailVo.class);
			

			String attach = reOrderDetailVo.getAttach();
			if(attach != null && (!attach.equals(""))){
				JSONObject attachJson=JSONObject.parseObject(attach);
				reOrderDetailVo.setLables(attachJson.getString("lables"));
				reOrderDetailVo.setEvaluate(attachJson.getString("evaluate"));
				reOrderDetailVo.setGrade(attachJson.getString("grade"));
				reOrderDetailVo.setAttach(null);
			}
			//详细列表
			List<OrderDetailVo> detailVos = new ArrayList<OrderDetailVo>();
			
			boolean haveStation = false;
			boolean haveCharger = false;
			//遍历详细信息
			for (int i = 0; i < orderDetailArray.size(); i++) {
				
				//订单详细
				JSONObject orderDetailJson = orderDetailArray.getJSONObject(i);
				OrderDetailVo orderDetailVo = JsonUtils.JsonToObjectOfClass(orderDetailJson, OrderDetailVo.class);
				
				String serviceNo = orderDetailVo.getServiceNo();
				
				//解析message  
				String messagearray =orderDetailVo.getMessage();
				if(messagearray!=null&&messagearray.length()>0){
					
					LOG.info("获取到订单详情message=【"+orderDetailVo.getMessage()+"】");
					JSONArray message = JSON.parseArray(orderDetailVo.getMessage());
					List<PaymentDetail> details= Lists.newArrayList();
					
					for(int p=0;p<message.size();p++){
						PaymentDetail detail = JsonUtils.JsonToObjectOfClass(message.getJSONObject(p), PaymentDetail.class);
						details.add(detail);
					}
					orderDetailVo.setDetails(details);
					
					
				}
				
				//如果为预约服务
				if(OrderServiceType.RESERVATION_SERVICE.getCode().equals(serviceNo)){
					
					//查询此订单的预约记录
					String resultReservationStr = omsService.getBespeakDetailInfoByOrderNo(orderNo).toString();

					//json转换
					JSONObject resultReservationJsons = JSONObject.parseObject(resultReservationStr);
					String rCode = resultReservationJsons.getString(OmsServerResultField.CODE);

					LOG.info("获取订单为【" + orderNo + "】 的充电记录,OMS服务器返回结果为:" + retCode);
					
					//如果为成功
					if(OmsCode.SUCCESS.equals(rCode)){
						
						JSONObject resultReservationJson = resultReservationJsons.getJSONObject("pojo");
						
						//电桩
						JSONObject charger = resultReservationJson.getJSONObject("charger");
						//电站
						JSONObject station = resultReservationJson.getJSONObject("station");
						//充电记录
						JSONObject reservation = resultReservationJson.getJSONObject("bespeak");
						
						if(charger!=null){
							if(!haveCharger){
								haveCharger = true;
								ChargerDetailVo chargerDetailVo = JsonUtils.JsonToObjectOfClass(charger, ChargerDetailVo.class);
								reOrderDetailVo.setChargerDetail(chargerDetailVo);
							}
							//如果没有取到纪录，通过设备id查询
				}else{
							if(!haveCharger){
								
								//TODO
								String resultJsonString = omsService.getChargerDetailByChargerId(orderDetailVo.getEquipmentNo()).toString();
								
								// json转换
								JSONObject omsResultObject = JSONObject.parseObject(resultJsonString);
		
								// 获取操作结果
								String code = omsResultObject.getString(Constants.OmsServerResultField.CODE);
		
								LOG.info("OMS获取 【" + orderDetailVo.getEquipmentNo() + "】 电桩详细,接口返回代码:" + code);
								LOG.info("获取到桩结果："+resultJsonString);
								// 操作成功
								if (OmsCode.SUCCESS.equals(code)) {
									
									JSONObject chargerString = omsResultObject.getJSONObject("pojo");		
									// 封装字段
									if(chargerString!=null){
										ChargerDetailVo chargerDetailVo = JsonUtils.JsonToObjectOfClass(chargerString, ChargerDetailVo.class);
										reOrderDetailVo.setChargerDetail(chargerDetailVo);
										
										haveCharger = true;
									}
								}
								
							}
							
						}
						if(station!=null){
							if(!haveStation){
								haveStation = true;
								StationDetailVo stationDetailVo = JsonUtils.JsonToObjectOfClass(station, StationDetailVo.class);
								reOrderDetailVo.setStationDetail(stationDetailVo);
							}
						}
						if(reservation!=null){
							ReservationDetailVo reservationDetailVo = JsonUtils.JsonToObjectOfClass(reservation, ReservationDetailVo.class);
							reOrderDetailVo.setReservationDetail(reservationDetailVo);
						}
						
					}
				}
				//如果为充电服务
				else if(OrderServiceType.AGENT_ELECTRIC_CHARGE_SERVICE.getCode().equals(serviceNo)){


					String resultChargerStr = omsService.getChargeDetailInfoByOrderNo(orderNo).toString();
					
					//json转换
					JSONObject resultChargerJsons = JSONObject.parseObject(resultChargerStr);
					String rCode = resultChargerJsons.getString(OmsServerResultField.CODE);

					LOG.info("获取订单为【" + orderNo + "】 的充电记录,OMS服务器返回结果为:" + retCode);
					
					//如果为成功
					if(OmsCode.SUCCESS.equals(rCode)){
						
						JSONObject resultChargerJson = resultChargerJsons.getJSONObject("pojo");
						//电桩
						JSONObject charger = resultChargerJson.getJSONObject("charger");
						//电站
						JSONObject station = resultChargerJson.getJSONObject("station");
						//充电记录
						JSONObject charge = resultChargerJson.getJSONObject("charge");
						
						if(charger!=null){
							if(!haveCharger){
								haveCharger = true;
								ChargerDetailVo chargerDetailVo = JsonUtils.JsonToObjectOfClass(charger, ChargerDetailVo.class);
								reOrderDetailVo.setChargerDetail(chargerDetailVo);
							}
						}else{
							if(!haveCharger){
								
								//TODO
								String resultJsonString = omsService.getChargerDetailByChargerId(orderDetailVo.getEquipmentNo()).toString();
								
								// json转换
								JSONObject omsResultObject = JSONObject.parseObject(resultJsonString);
		
								// 获取操作结果
								String code = omsResultObject.getString(Constants.OmsServerResultField.CODE);
		
								LOG.info("OMS获取 【" + orderDetailVo.getEquipmentNo() + "】 电桩详细,接口返回代码:" + code);
								LOG.info("获取到桩结果："+resultJsonString);
								// 操作成功
								if (OmsCode.SUCCESS.equals(code)) {
									
									JSONObject chargerString = omsResultObject.getJSONObject("pojo");		
									// 封装字段
									if(chargerString!=null){
										ChargerDetailVo chargerDetailVo = JsonUtils.JsonToObjectOfClass(chargerString, ChargerDetailVo.class);
										reOrderDetailVo.setChargerDetail(chargerDetailVo);
										haveCharger = true;
									}
								}
								
							}
							
						}
						if(station!=null){
							if(!haveStation){
								haveStation = true;
								StationDetailVo stationDetailVo = JsonUtils.JsonToObjectOfClass(station, StationDetailVo.class);
								reOrderDetailVo.setStationDetail(stationDetailVo);
							}
						}
						if(charge!=null){
							ChargeDetailVo chargeDetailVo = JsonUtils.JsonToObjectOfClass(charge, ChargeDetailVo.class);
							reOrderDetailVo.setChargeDetail(chargeDetailVo);
						}else{
							//
							ChargeDetailVo chargeDetailVo = new ChargeDetailVo();
							
							String portNo = orderDetailJson.getString("auxEquipmentNo");
							if(portNo==null){
								portNo="1";
							}
							chargeDetailVo.setPortNo(portNo);
							reOrderDetailVo.setChargeDetail(chargeDetailVo);
						}
						
					}
				}
				detailVos.add(orderDetailVo);
			}
			reOrderDetailVo.setOrderDetails(detailVos);

			returnJsonObject.put("orderDetail", reOrderDetailVo);
		}
		else{
			returnJsonObject = JsonUtils.getFailJson(Response.RUNTIME_ERROR);
		}
		return returnJsonObject;
	}

	@Override
	public JSONObject balanceAccounts(String userId, String orderNo, String cardNo, String cardType) throws AvroRemoteException {
		
		JSONObject returnJsonObject = null;
		
		//查询订单详细
		String resultCharSequence = orderService.findOrderItemsByOrderNo(orderNo).toString();

		System.out.println(resultCharSequence);
		JSONObject orderResultObject = JSONObject.parseObject(resultCharSequence);
		String retCode = orderResultObject.getString("retCode");

		LOG.info("获取【" + orderNo + "】 订单详细,订单服务器返回结果为:" + retCode);
		
		// 操作
		if (OrderOpResult.SUCCESS.getCode().equals(retCode)) {
			
			//详细json数组
			JSONObject resultJson  = orderResultObject.getJSONObject("bizData");
			
			OrderInfoVo orderVo = JsonUtils.JsonToObjectOfClass(resultJson, OrderInfoVo.class);
			String status = orderVo.getStatus();
			String userCard = orderVo.getCardNo();
			String payMoney = orderVo.getCaculatedTotalPrice();
			
			if(StringUtils.isEmpty(userCard)){
				throw new IllegalStateException("此订单用户卡号为空");
			}
			if(StringUtils.isEmpty(status) || Integer.parseInt(status)!=0){//如果不是待支付状态
				throw new IllegalStateException("此订单已完成");
			}
			else{
				
				//主服务器扣费
				Map<String, String> params = new HashMap<String,String>();
				params.put("cardNo", userCard);
				params.put("paymentAmount", payMoney);
				String resultMainStr = msService.deductOnlineAccountBal(JSON.toJSONString(params)).toString();
				
				LOG.info("用户卡号为【 "+userCard+" 】 开始MS扣款,扣款金额为:【 "+payMoney+" 】,MS返回结果为:"+resultMainStr);
				//返回结果
				JSONObject mainResultJson = JSONObject.parseObject(resultMainStr);
				
				int opResult = mainResultJson.getIntValue(Constants.MainServerResultField.OP_RESULT);
				
				//扣款成功
				if(opResult==1){

					Map<String,String> payInfoParams = new HashMap<String,String>();
					payInfoParams.put("cardNo", cardNo);
					payInfoParams.put("cardType", cardType);
					payInfoParams.put("userId", userId);
					payInfoParams.put("orderNo", orderNo);
					payInfoParams.put("payTypeCode", "4");//4为电子钱包支付
					String payResultString =  orderService.userPaying(JSON.toJSONString(payInfoParams)).toString();
					
					JSONObject payResultObject = JSONObject.parseObject(payResultString);
					String payCode = payResultObject.getString("retCode");

					LOG.info("获取【" + orderNo + "】 订单详细,订单服务器返回结果为:" + retCode);
					
					// 操作
					if (OrderOpResult.SUCCESS.getCode().equals(payCode)) {
						returnJsonObject = JsonUtils.getSuccessJson();
					}
					
				}
				//扣款失败
				else{
					String code = mainResultJson.getString(Constants.MainServerResultField.ERROR_CODE);
					returnJsonObject = JsonUtils.getFailJson(CodeUtils.getCodeByMainServerCode(code));
				}
			}
			
		}
		else{
			returnJsonObject = JsonUtils.getFailJson(Response.RUNTIME_ERROR);
		}
		
		return returnJsonObject;
	}

	@Override
	public JSONObject getReservationDiscount(String userCard, String portNo, String chargerId, String minute,
			String startTime) throws AvroRemoteException {
		
		JSONObject returnJsonObject = null;
		
		//查询订单详细
		String resultCharSequence = omsService.getReservationDiscount(userCard, portNo, chargerId, minute, startTime).toString();

		JSONObject orderResultObject = JSONObject.parseObject(resultCharSequence);
		String retCode = orderResultObject.getString("retCode");

		LOG.info("获取预约折扣信息,OMS服务器返回结果为:" + retCode);
		
		// 操作
		if (OrderOpResult.SUCCESS.getCode().equals(retCode)) {
			
			returnJsonObject = JsonUtils.getSuccessJson();
			
		}
		else{
			Response response = CodeUtils.getCodeByOMSServerCode(retCode);
			returnJsonObject = JsonUtils.getFailJson(response);
		}
		
		return returnJsonObject;
	}

	@Override
	public ResultMappingEntity getUserNeedPayOrders(String userId) throws AvroRemoteException {
		
		
		ResultMappingEntity resultMappingEntity = null;
		
		//查询订单详细
		String resultCharSequence = orderService.findUserNeedPayOrders(userId).toString();

		JSONObject orderResultObject = JSONObject.parseObject(resultCharSequence);
		String retCode = orderResultObject.getString("retCode");

		LOG.info("获取【 "+userId+" 】用户未支付的订单,OS服务器返回结果为:" + resultCharSequence);
		
		List<String> orderNos = new ArrayList<String>();
		// 操作
		if (OrderOpResult.SUCCESS.getCode().equals(retCode)) {

			//详细json数组
			JSONArray resultJson  = orderResultObject.getJSONArray("bizData");
			for (int i = 0; i < resultJson.size(); i++) {
				JSONObject jsonObject = resultJson.getJSONObject(i);
				String orderNo = jsonObject.getString("orderNo");
				orderNos.add(orderNo);
			}
			resultMappingEntity = Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg(), orderNos);
			
		}
		else{
			Response response = CodeUtils.getCodeByOMSServerCode(retCode);
			resultMappingEntity = Result.responseBody(response.getCode(), response.getMsg(), orderNos);
		}
		
		return resultMappingEntity;
	}


	@Override
	public ResultMappingEntity getProductOrder(String orderNo,String appId, String body,String payType,
			String openId,String ip) throws AvroRemoteException {
		
		
		ResultMappingEntity resultMappingEntity = null;
		Map<String, String> params = new HashMap<String,String>();
		params.put("orderno", orderNo);
		params.put("openId", openId);
		params.put("appId", appId);
		params.put("body", body);
		params.put("ip", ip);
		LOG.info("传入参数orderno="+orderNo+";openId="+openId+";appId="+appId+";body="+body+";ip="+ip);
		String resultPayOrder = orderService.Order2WeiXinOrder(JSON.toJSONString(params)).toString();//WeiXinUtils.payUnifiedorder(vo, source, merchantCode);
		LOG.info("生成订单返回结果["+resultPayOrder+"]");
		JSONObject orderResultObject = JSONObject.parseObject(resultPayOrder);
		String retCode=orderResultObject.getString("retCode");
		if(retCode!=null&&retCode.equals("success")){
			resultMappingEntity = Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg(), orderResultObject);
		}else{
			resultMappingEntity = Result.responseBody("0",Response.RUNTIME_ERROR.getCode(), orderResultObject.getString("msg"));
		}
		return resultMappingEntity;
		
	}


	@Override
	public ResultMappingEntity orderAttach(String userId , String orderNo, String lables, String evaluate,String grade) throws AvroRemoteException {
		
		ResultMappingEntity  resultMappingEntity=null;
		Map<String,String> params = new HashMap<String,String>();
		LOG.info("添加附加信息传人订单id【"+orderNo+"】标签是【"+lables+"】评价【"+evaluate+"】");
		params.put("orderNo", orderNo);
		JSONObject  obj= new JSONObject();
		obj.put("lables", lables);
		obj.put("evaluate", evaluate);
		obj.put("userId", userId);
		obj.put("grade", grade);
		String attach=JSON.toJSONString(obj);
		params.put("attach", attach);

		String payResultString =  orderService.orderAttach(JSON.toJSONString(params)).toString();
		LOG.info("添加附加信息返回的结果"+payResultString+"；");
		JSONObject orderResultObject = JSONObject.parseObject(payResultString);
		String retCode = orderResultObject.getString("retCode");
		if (OrderOpResult.SUCCESS.getCode().equals(retCode)) {			
			resultMappingEntity = Result.responseBody(Response.SUCCESS.getCode(),Response.SUCCESS.getMsg());			
		}else{
			resultMappingEntity = Result.responseBody(Response.FIAL.getCode(),Response.FIAL.getMsg(), orderResultObject.getString("errorMsg"));
		}
		return resultMappingEntity;
	}
	
	public ResultMappingEntity deleteOrder(String orderNo ,String showHide) throws AvroRemoteException {
		
		ResultMappingEntity  resultMappingEntity=null;
		Map<String,String> params = new HashMap<String,String>();
		LOG.info("删除订单id【"+orderNo+"】");
		params.put("orderNo", orderNo);
		params.put("hide",showHide);
		String payResultString =  orderService.orderAttach(JSON.toJSONString(params)).toString();
		LOG.info("返回的结果"+payResultString+"；");
		JSONObject orderResultObject = JSONObject.parseObject(payResultString);
		String retCode = orderResultObject.getString("retCode");
		if (OrderOpResult.SUCCESS.getCode().equals(retCode)) {
			resultMappingEntity = Result.responseBody(Response.SUCCESS.getCode(),Response.SUCCESS.getMsg());			
		}else{
			resultMappingEntity = Result.responseBody(Response.FIAL.getCode(),Response.FIAL.getMsg(), orderResultObject.getString("errorMsg"));
		}
		return resultMappingEntity;	
	}


	@Override
	public ResultMappingEntity scoreStation(String userId, String stationId, String score) throws AvroRemoteException {
		
		LOG.info("评分的用户id["+userId+"]站点id=["+stationId+"]分数score=["+score+"]");
		String resultJsonString=omsService.scoreStation(userId, stationId, score, DateUtil.getDateTime()).toString();
		LOG.info("评分返回结果：【"+resultJsonString+"】");
		// json转换
		JSONObject omsResultObject = JSONObject.parseObject(resultJsonString);

		// 获取操作结果
		String code = omsResultObject.getString(Constants.OmsServerResultField.CODE);
		ResultMappingEntity  resultMappingEntity=null;
		// 操作成功
		if (OmsCode.SUCCESS.equals(code)) {
			
			  resultMappingEntity = Result.responseBody(Response.SUCCESS.getCode(),Response.SUCCESS.getMsg());
		}else{
			
			resultMappingEntity = Result.responseBody(Response.FIAL.getCode(),Response.FIAL.getMsg());
		}
		return resultMappingEntity;
	}
}

